package com.example.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.domain.TuShu;
import com.example.service.TuShuService;
import com.example.util.HttpUtil;

public class TuShuServiceImpl implements TuShuService {

	@Override
	public List<TuShu> getTuShus() {
		String servlet = "TuShuServlet";
		String method = "getTuShus";
		String result = HttpUtil.sendHttp(servlet, method, null);
		List<TuShu> list = new ArrayList<TuShu>();
		if (!"false".equals(result)) {
			try {
				JSONObject jsonObject = new JSONObject(result);
				JSONArray jsonArray = jsonObject.getJSONArray("getTuShus");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject object = jsonArray.getJSONObject(i);
					TuShu tuShu = new TuShu();
					tuShu.setJy_sj(object.getString("jy_sj"));
					tuShu.setTs_cbs(object.getString("ts_cbs"));
					tuShu.setTs_jj(object.getString("ts_jj"));
					tuShu.setTs_sm(object.getString("ts_sm"));
					tuShu.setTs_zz(object.getString("ts_zz"));
					list.add(tuShu);
				}
				return list;
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public TuShu geTuShu(String ts_sm) {
		String servlet = "TuShuServlet";
		String method = "getTuShu";
		Map<String, String> map = new HashMap<String, String>();
		map.put("ts_sm",ts_sm);
		String result = HttpUtil.sendHttp(servlet, method, map);
		if (!"false".equals(result)) {
			try {
				JSONObject jsonObject1 = new JSONObject(result);
				JSONObject jsonObject = jsonObject1.getJSONObject("getTuShu");
				TuShu tuShu = new TuShu();
				tuShu.setJy_sj(jsonObject.getString("jy_sj"));
				tuShu.setTs_cbs(jsonObject.getString("ts_cbs"));
				tuShu.setTs_jj(jsonObject.getString("ts_jj"));
				tuShu.setTs_sm(jsonObject.getString("ts_sm"));
				tuShu.setTs_zz(jsonObject.getString("ts_zz"));
				return tuShu;
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

}
